/**
 * @program: yygh_parent
 * @ClassName WeiXinController
 * @description:
 * @author: zyw
 * @create: 2023-06-09 12:20
 * @Version 1.0
 **/
package com.atguigu.yygh.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.enums.PaymentTypeEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.order.service.PaymentService;
import com.atguigu.yygh.order.service.WeixinService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RequestMapping("/user/order/weixin")
@RestController
public class WeiXinController {
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private OrderInfoService orderInfoService;


    //下单，生成二维码
    @GetMapping("/createNative/{orderId}")
    public R createNative(@PathVariable Long orderId) {
        String url = weixinService.createNative(orderId);
        return R.ok().data("url",url);
    }

    @GetMapping("/status/{orderId}")
    public R getPayStatus(@PathVariable Long orderId) {
        Map<String, String> resultMap = weixinService.queryPayStatus(orderId);
        if (resultMap == null) {//出错
            return R.error().message("查询出错");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))) {//如果成功
            weixinService.paySuccess(orderId,resultMap);
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中");
    }

}
 
